#!/usr/bin/env python
# encoding: utf-8
'''
@author: songyunlong
@contact: 1243049371@qq.com
@software: Pycharm
@file: Generator
@time: 2019/7/28 下午5:42
'''
import numpy as np
import tensorflow as tf

class Generator(tf.keras.models.Model):
    '''
    生成器
    '''
    def __init__(self):
        super(Generator, self).__init__()

    def call(self, inputs, **kwargs):
        g = tf.keras.layers.Dense(units=16 * 16)(inputs)
        g = tf.keras.layers.LeakyReLU()(g)
        g = tf.keras.layers.Reshape((16, 16, 1))(g)
        # 添加卷积层
        g = tf.keras.layers.Conv2D(filters=32, kernel_size=5, padding='same')(g)
        g = tf.keras.layers.LeakyReLU()(g)
        # 上采样至 32 x 32
        g = tf.keras.layers.Conv2DTranspose(filters=64, kernel_size=4, strides=2, padding='same')(g)
        g = tf.keras.layers.LeakyReLU()(g)
        # 添加卷积层
        g = tf.keras.layers.Conv2D(filters=64, kernel_size=5, padding='same')(g)
        g = tf.keras.layers.LeakyReLU()(g)
        g = tf.keras.layers.Conv2D(filters=128, kernel_size=5, padding='same')(g)
        g = tf.keras.layers.LeakyReLU()(g)
        g = tf.keras.layers.Flatten()(g)
        g = tf.keras.layers.Dense(units=104, activation='tanh')(g)
        return g

class Discriminator(tf.keras.models.Model):
    '''
    判别器
    '''
    def __init__(self):
        super(Discriminator, self).__init__()

    def call(self, inputs, **kwargs):
        d = tf.keras.layers.Dense(units=225, activation='relu')(inputs)
        d = tf.keras.layers.Reshape((15, 15, 1))(d)
        d = tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=2, padding='same')(d)
        d = tf.keras.layers.LeakyReLU()(d)
        d = tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(d)
        d = tf.keras.layers.LeakyReLU()(d)
        d = tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(d)
        d = tf.keras.layers.LeakyReLU()(d)
        d = tf.keras.layers.Conv2D(filters=128, kernel_size=3, strides=2, padding='same')(d)
        d = tf.keras.layers.LeakyReLU()(d)
        d = tf.keras.layers.Flatten()(d)
        d = tf.keras.layers.Dropout(0, 4)(d)
        d = tf.keras.layers.Dense(units=1, activation='sigmoid')(d)
        return d

class DiscriminatorGan(tf.keras.models.Model):
    '''
    对生成数据的判别器
    '''
    def __init__(self):
        super(DiscriminatorGan, self).__init__()
        self.__generator = Generator()
        self.__discriminator = Discriminator()

    def call(self, inputs, **kwargs):
        self.__discriminator.trainable = False
        return self.__discriminator(self.__generator(inputs))

if __name__ == '__main__':
    pass












